﻿/*
 * File: TossScene.cs
 * Author: Gourav Das
 * Purpose: Scene for Toss. In POC this is 2D screen, in later versions this is 3D
 * Created: May 02 2011
 */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.Xml;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the Toss scene
    /// </summary>
    class TossScene : BaseMenuScene
    {
        ContentManager content;
        float pauseAlpha;
        bool UserTeamToCall = false;
        bool yetToCall = true;
        bool batnBowlDecided = false;
        string TossMsg = string.Empty;

        /// <summary>
        /// Constructor.
        /// </summary>
        public TossScene(string title)
            : base(title)
        {
            // Create our menu entries.
            MenuItem ProceedMenuItem = new MenuItem("Proceed", 0, false, true);
            MenuItem BackMenuItem = new MenuItem("Back", 1, true);

            // Hook up menu event handlers.
            ProceedMenuItem.Selected += ProceedMenuItem_Selected;
            BackMenuItem.Selected += BackMenuItem_Selected;

            // Add Items to the menu.
            MenuItems.Add(ProceedMenuItem);
            MenuItems.Add(BackMenuItem);

            TransitioningTime = TimeSpan.FromSeconds(0.5);
            TransitionedTime = TimeSpan.FromSeconds(0.5);

            //Clear Dynamic MessageStore of all messages of Toss category
            List<Message> tmpmsg = new List<Message>();
            foreach (Message msg in Game22Yards.DynMsgStore.Messages)
            {
                if (msg.Category != "Toss")
                    tmpmsg.Add(msg);
            }
            Game22Yards.DynMsgStore.Messages.Clear();
            Game22Yards.DynMsgStore.Messages = tmpmsg;

            tmpmsg.Clear();

            foreach (Message msg in Game22Yards.matchPlayEngine.MessageStore.Messages)
            {
                if (msg.Category != "Toss")
                    tmpmsg.Add(msg);
            }
            Game22Yards.matchPlayEngine.MessageStore.Messages.Clear();
            Game22Yards.matchPlayEngine.MessageStore.Messages = tmpmsg;

        }

        /// <summary>
        /// Handles the Selected event of the ProceedMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="_22yards.Scenes.Helpers.PlayerIndexEventArgs"/> instance containing the event data.</param>
        void ProceedMenuItem_Selected(object sender, PlayerIndexEventArgs e)
        {
            ExitScene();
            LoadingScene.Load(sceneEngine, true, ControllingPlayer,new GamePlayBaseScene());
            //sceneEngine.AddScene(new TwoDeeGamePlayBaseScene(), ControllingPlayer);
        }


        /// <summary>
        /// Event handler for when the Back Menu is selected.
        /// </summary>
        void BackMenuItem_Selected(object sender, PlayerIndexEventArgs e)
        {

            ExitScene();
            LoadingScene.Load(sceneEngine, true, ControllingPlayer, new BackgroundScene(), new Playing12SelectionScene("Players Selection"));
            //sceneEngine.AddScene(new Playing12SelectionScene("Players Selection"), ControllingPlayer);
        }

        /// <summary>
        /// Load graphics and non-graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(sceneEngine.Game.Services, "22yardsContent");

            Random rnd = new Random();
            if (rnd.Next(0, 2) == 0)
            {
                UserTeamToCall = false;
                if (Game22Yards.matchPlayEngine.match.Team1.IsUserSelected)
                    TossMsg = Game22Yards.matchPlayEngine.match.Team2.TeamName + " to call. Press T for Toss";
                else
                    TossMsg = Game22Yards.matchPlayEngine.match.Team1.TeamName + " to call. Press T for Toss";
            }
            else
            {
                UserTeamToCall = true;
                if (Game22Yards.matchPlayEngine.match.Team1.IsUserSelected)
                    TossMsg = "Your team " + Game22Yards.matchPlayEngine.match.Team1.TeamName + " to call. Press H for Heads";
                else
                    TossMsg = "Your team " + Game22Yards.matchPlayEngine.match.Team2.TeamName + " to call. Press T for Tails";

            }

            // If loading is slow, uncomment below line and use LoadingScene in previous screen to load this screen
            //sceneEngine.Game.ResetElapsedTime();

        }


        /// <summary>
        /// Unload graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }

        /// <summary>
        /// Responds to user input, moving through menu items and accepting or cancelling the menu.
        /// </summary>
        /// <param name="_input"></param>
        public override void HandleInput(InputEngine _input)
        {

            // Accept or cancel the menu? We pass in our ControllingPlayer,
            PlayerIndex playerIndex;

            int hoverIndex = GetMenuEntryAt(new Vector2(_input.LastMouseStates.X, _input.LastMouseStates.Y));
            if (hoverIndex > -1 && _input.MousePositionChanged(ControllingPlayer, out playerIndex))// IsMouseLeftButtonPress(ControllingPlayer,out playerIndex))
            {
                SelectedItem = hoverIndex;
            }
            else if (hoverIndex < 0 && _input.MousePositionChanged(ControllingPlayer, out playerIndex))// IsMouseLeftButtonPress(ControllingPlayer,out playerIndex))
            {
                SelectedItem = -1;
            }
            else //if (!_input.MousePositionChanged(ControllingPlayer, out playerIndex) && hoverIndex <0)
            {
                // Move to the previous menu item
                if (_input.IsMenuUp(ControllingPlayer) && !_input.MousePositionChanged(ControllingPlayer, out playerIndex))
                {
                    SelectedItem--;

                    //Cycle through Menu Items
                    if (SelectedItem < 0)
                        SelectedItem = MenuItems.Count - 1;
                }

                // Move to the next menu item
                if (_input.IsMenuDown(ControllingPlayer) && !_input.MousePositionChanged(ControllingPlayer, out playerIndex))
                {
                    SelectedItem++;

                    //Cycle through Menu Items
                    if (SelectedItem >= MenuItems.Count)
                        SelectedItem = 0;
                }

                if (_input.IsMenuSelect(ControllingPlayer, out playerIndex) && !_input.MousePositionChanged(ControllingPlayer, out playerIndex))
                {
                    OnSelectEntry(SelectedItem, playerIndex);
                }
                else if (_input.IsMenuCancel(ControllingPlayer, out playerIndex) && !_input.MousePositionChanged(ControllingPlayer, out playerIndex))
                {
                    OnCancel(playerIndex);
                }
            }

            if (UserTeamToCall && yetToCall)
            {

                //KeyboardState ks = new KeyboardState();
                //Random rnd = new Random();
                if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.T))
                {
                    Game22Yards.matchPlayEngine.TossCoin(1);
                    yetToCall = false;
                    TossMsg = "You called Tails";
                }
                else if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.H))
                {
                    Game22Yards.matchPlayEngine.TossCoin(0);
                    yetToCall = false;
                    TossMsg = "You called Heads";
                }

            }
            else if (!UserTeamToCall && yetToCall)
            {
                //KeyboardState ks = new KeyboardState();
                if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.T))
                {
                    Random rnd = new Random();
                    if (rnd.Next(0, 2) == 0)
                    {
                        Game22Yards.matchPlayEngine.TossCoin(0);
                        TossMsg = "Heads is the call";
                    }
                    else
                    {
                        Game22Yards.matchPlayEngine.TossCoin(1);
                        TossMsg = "Tails is the call";
                    }
                    yetToCall = false;
                }
            }

            if(!yetToCall)
            {
                //KeyboardState ks = new KeyboardState();
                Random rnd = new Random();
                if (Game22Yards.matchPlayEngine.match.Team1.TossWinner && Game22Yards.matchPlayEngine.match.Team1.IsUserSelected && !batnBowlDecided)
                {
                    TossMsg = "You team won the Toss. Press B to Bat first. O to bowl first.";
                    if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.B))
                    {
                        batnBowlDecided = true;
                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[10].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[9].IsSetToBowlNext = true;

                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = false;
                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings;i=i+2 )
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;

                        TossMsg = "You selected to bat first.";
                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bat first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bat first."));
                    }
                    else if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.O))
                    {
                        batnBowlDecided = true;
                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = false;
                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[10].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[9].IsSetToBowlNext = true;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;


                        TossMsg = "You selected to bowl first.";
                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bowl first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bowl first."));

                    }
                }
                else if (Game22Yards.matchPlayEngine.match.Team1.TossWinner && !Game22Yards.matchPlayEngine.match.Team1.IsUserSelected && !batnBowlDecided)
                {

                    if (rnd.Next(0, 2) == 0)
                    {
                        batnBowlDecided = true;
                        TossMsg = Game22Yards.matchPlayEngine.match.Team1.TeamName + "won the Toss and elected to bowl first.";
                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = false;
                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[8].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[10].IsSetToBowlNext = true;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;


                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bowl first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bowl first."));
                        
                    }
                    else
                    {
                        batnBowlDecided = true;
                        TossMsg = Game22Yards.matchPlayEngine.match.Team1.TeamName + "won the Toss and elected to bat first.";

                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[10].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[8].IsSetToBowlNext = true;

                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = false;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;


                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bat first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team1.TeamShortName + " won the toss and elected to bat first."));

                    }
                }
                else if (Game22Yards.matchPlayEngine.match.Team2.TossWinner && Game22Yards.matchPlayEngine.match.Team2.IsUserSelected && !batnBowlDecided)
                {
                    TossMsg = "You team won the Toss. Press B to Bat first. O to bowl first.";

                    if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.B))
                    {
                        batnBowlDecided = true;
                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[6].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[9].IsSetToBowlNext = true;

                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = false;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;


                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bat first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bat first."));
                        TossMsg = "You selected to bat first.";
                    }
                    else if (_input.CurrentKeyboardStates[(int)ControllingPlayer.Value].IsKeyDown(Keys.O))
                    {
                        batnBowlDecided = true;
                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = false;
                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[8].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[6].IsSetToBowlNext = true;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;


                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bowl first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bowl first."));
                        TossMsg = "You selected to bowl first.";
                    }
                }
                else if (Game22Yards.matchPlayEngine.match.Team2.TossWinner && !Game22Yards.matchPlayEngine.match.Team2.IsUserSelected && !batnBowlDecided)
                {
                    if (rnd.Next(0, 2) == 0)
                    {
                        batnBowlDecided = true;
                        TossMsg = Game22Yards.matchPlayEngine.match.Team2.TeamName + "won the Toss and elected to bowl first.";

                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = false;
                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[5].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[6].IsSetToBowlNext = true;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;


                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bowl first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bowl first."));
                    
                    }
                    else
                    {
                        batnBowlDecided = true;
                        TossMsg = Game22Yards.matchPlayEngine.match.Team2.TeamName + "won the Toss and elected to bat first.";

                        Game22Yards.matchPlayEngine.match.Team2.FirstBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.CurrentTeamBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOnStrike = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[0].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsBatting = true;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOnStrike = false;
                        Game22Yards.matchPlayEngine.match.Team2.players[1].IsOut = false;
                        Game22Yards.matchPlayEngine.match.Team1.players[8].IsBowlingCurrently = true;
                        Game22Yards.matchPlayEngine.match.Team1.players[7].IsSetToBowlNext = true;
                        Game22Yards.matchPlayEngine.match.Team1.FirstBatting = false;
                        Game22Yards.matchPlayEngine.match.Team1.CurrentTeamBatting = false;

                        for (int i = 1; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team2.TeamName;
                        for (int i = 2; i <= Game22Yards.matchPlayEngine.match.matchScore.NumInnings; i = i + 2)
                            Game22Yards.matchPlayEngine.match.matchScore.NthInningsScore(i).TeamName = Game22Yards.matchPlayEngine.match.Team1.TeamName;


                        Game22Yards.DynMsgStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bat first."));
                        Game22Yards.matchPlayEngine.MessageStore.Messages.Add(new Message("Toss", Game22Yards.matchPlayEngine.match.Team2.TeamShortName + " won the toss and elected to bat first."));

                    }
                }
            }

        }


        /// <summary>
        /// Updates the state of the game. This method checks the GameScreen.IsActive
        /// property, so the game will stop updating when the pause menu is active,
        /// or if you tab away to a different application.
        /// </summary>
        public override void Update(GameTime _gameTime, bool _otherSceneHasFocus,
                                                       bool _coveredByOtherScene)
        {
            base.Update(_gameTime, _otherSceneHasFocus, false);

            // Gradually fade in or out depending on whether we are covered by the pause screen.
            if (_coveredByOtherScene)
                pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
            else
                pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);

            if (IsActive)
            {
                //base.Update(_gameTime, _otherScreenHasFocus, _coveredByOtherScreen);

                // Update each nested MenuEntry object.
                for (int i = 0; i < MenuItems.Count; i++)
                {
                    bool isSelected = IsActive && (i == SelectedItem);

                    MenuItems[i].Update(this, isSelected, _gameTime);
                }

            }

        }


        /// <summary>
        /// Draws the Scene with the menus.
        /// </summary>
        public override void Draw(GameTime _gameTime)
        {
            UpdateMenuItemLocations();

            sceneEngine.SpriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < MenuItems.Count; i++)
            {
                MenuItem menuItem = MenuItems[i];

                bool isSelected = IsActive && (i == SelectedItem);

                menuItem.Draw(this, isSelected, _gameTime);
            }

            // Make the menu title slide into place during transitions, this makes
            // the movement slow down as it nears the end
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            // Draw the menu title centered on the screen
            Vector2 titlePosition = new Vector2(sceneEngine.GraphicsDevice.Viewport.Width / 2, ((float)80 * Game22Yards.scaleFactor.htScale));
            Vector2 titleOrigin = sceneEngine.TxtNFont.spFontGoodTimes.MeasureString(MenuTitle) / 2;
            Vector2 infoPos = new Vector2(((float)600 * Game22Yards.scaleFactor.widthScale), ((float)400 * Game22Yards.scaleFactor.htScale));

            titlePosition.Y -= transitionOffset * 100;
            infoPos.Y += transitionOffset * 300;

            //Draw Menu Title
            sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontGoodTimes, MenuTitle, titlePosition, Color.White, 0,
                                   titleOrigin, 1.5f, SpriteEffects.None, 0);

            sceneEngine.SpriteBatch.DrawString(sceneEngine.TxtNFont.spFontPescadero, TossMsg, infoPos, Color.White);

            sceneEngine.SpriteBatch.End();
        }


        /// <summary>
        /// When the user presses ESC, do nothing.
        /// </summary>
        protected override void OnCancel(PlayerIndex _playerIndex)
        {
            //Do nothing :)
        }
    }
}
